<template>
  <div class="update-gender">
    <van-picker
      show-toolbar
      :columns="columns"
      :default-index="defaultIndex"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="onGenderChange"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  components: {},
  props: {
    value: {
      type: Number,
      default: 1,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      defaultIndex: this.value
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {},
  mounted () {
    console.log(this.user, '555')
  },
  methods: {
    onGenderChange (picker, value, index) {
      this.defaultIndex = index
    },
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidclick: true
      })
      await this.$http.put('api/user/logoInfo', {
        data: {
          gender: this.defaultIndex,
          username: this.user.username
        }
      }).then(({ data: res }) => {
        if (res.code === 0) {
          const data = {
            ...this.user,
            gender: this.defaultIndex
          }
          this.$emit('input', this.defaultIndex)
          this.$emit('close')
          this.$store.commit('setUser', data)
          this.$toast.success('保存成功')
        } else {
          this.$toast.fail('修改失败')
        }
      })
    }
  }
}
</script>

<style scoped lang="less"></style>
